<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="identityCard">
        <el-input
          v-model="queryParams.identityCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属关系" prop="ownership">
        <el-select v-model="queryParams.ownership" placeholder="所属关系" clearable>
          <el-option
            v-for="dict in dict.type.ownership"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属老人" prop="elderlyInfoId">
        <el-select
          v-model="queryParams.elderlyInfoId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="optionsLoading"
          @change="onSelectChange"
        >
          <el-option
            v-for="item in options"
            :key="item.elderlyInfoId"
            :label="item.name"
            :value="item.elderlyInfoId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="tableData"
      row-key="menuId"
    >
      <el-table-column prop="name" label="姓名" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="age" label="年龄" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="gender" label="性别" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="identityCard" label="身份证" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="ownership" label="所属关系" minWidth="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ownership" :value="scope.row.ownership"/>
        </template>
      </el-table-column>
      <el-table-column prop="elderlyName" label="老人" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="phone" label="电话号码" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="remark" label="备注" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:menu:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改菜单对话框 -->
    <AddModal
      v-model="addVisible"
      :detail="row"
      @refresh="handleQuery"
    />
    <ImportModal v-model="importVisible" />
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
      :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryList } from "@/api/pension/elderlyInfo";
import { queryPage } from "@/api/pension/supervisionInfo";
import AddModal from "./components/add-modal.vue";
import ImportModal from "./components/import-modal.vue";
import { getToken } from "@/utils/auth";

export default {
  name: "SupervisePerson",
  dicts: ['ownership'],
  components: { AddModal, ImportModal },
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/pension/supervisionInfo/importData"
      },
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        name: undefined,
        identityCard: undefined,
        elderlyInfoId:'',
        ownership: ''
      },
      addVisible: false,
      importVisible: false,
      row: null,
      tableData: [],
      options: [],
      total: 0,
      optionsLoading: false
    };
  },
  created() {
    this.getList();
    this.remoteMethod();
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('pension/supervisionInfo/importTemplate', {
      }, `supervisionInfoTemplate_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      // this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 远程搜索
    remoteMethod(query) {
      if (query !== '') {
        this.optionsLoading = true;
        queryList({name:query}).then(response => {
          this.options = response.data;
          this.optionsLoading = false;
        });
      } else {
        this.options = [];
      }
    },
    onSelectChange(val) {
      const option = this.options.find(item => item.value === val)

      if (option) {
        // ....
      }else{

      }
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      queryPage(this.queryParams).then(response => {
        this.tableData = response.data.list;
        this.total = response.data.totalCount;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.remoteMethod();
    },
    handleAdd() {
      this.row = null
      this.addVisible = true
    },
    // handleImport() {
    //   this.importVisible = true
    // },
    handleUpdate(row) {
      this.row = row
      this.addVisible = true
    }
  }
};
</script>
